/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.entities.inventory.item;

import static engine.entities.inventory.item.InventoryTable.ITEM_RENDER_SCALE;
import engine.entities.inventory.item.InventoryTable.ItemTransferHandler;
import engine.game.gfx.ColoredSprite;
import engine.game.hud.SpriteRenderer;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.TransferHandler;
import javax.swing.border.Border;

/**
 *
 * @author NathanT
 */
public class InventoryItemRenderer extends SpriteRenderer {

    private final Font font = new Font("Arial", Font.BOLD, 12);
    private final ItemTransferHandler handler = new ItemTransferHandler();
    private InventoryItem invItem;
    private ImageIcon defaultIcon;
    private final MobEquipSet.EQUIP_SLOT slot;
    private ItemRendererListener listener;

    public InventoryItemRenderer(InventoryItem item, ColoredSprite defaultSprite, MobEquipSet.EQUIP_SLOT slot) {
        super(defaultSprite, ITEM_RENDER_SCALE);
        this.slot = slot;
        this.defaultIcon = defaultSprite.getImageIcon(ITEM_RENDER_SCALE, this.getBackground());
        this.setOpaque(true);
        Border border = BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(), BorderFactory.createEmptyBorder(5, 5, 5, 5));
        this.setBorder(border);
        this.setInvItem(item);
        this.setTransferHandler(handler);
        this.addMouseListener(new ItemMouseAdapter());
    }

    public void setDefaultIcon(ColoredSprite sprite, int scale) {
        this.defaultIcon = sprite.getImageIcon(scale, this.getBackground());
    }

    public final void setInvItem(InventoryItem invItem) {
        this.invItem = invItem;
        if (invItem != null) {
            Item item = invItem.getItem();
            if (item != null) {
                this.setToolTipText(invItem.getItem().getName());
                this.setIcon(invItem.getItem().getIcon());
            }
            else {
                this.setIcon(defaultIcon);
                this.setToolTipText(null);
            }
        }
        else {
            this.setIcon(defaultIcon);
            this.setToolTipText(null);
        }
        if (listener != null) {
            listener.itemChanged(invItem);
        }
    }

    public void setItemRendererListener(ItemRendererListener listener) {
        this.listener = listener;
    }

    public final InventoryItem getInvItem() {
        return invItem;
    }

    public final MobEquipSet.EQUIP_SLOT getSlot() {
        return slot;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (invItem == null) {
            return;
        }

        int quantity = invItem.getQuantity();
        if (quantity == 1) {
            return;
        }

        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setFont(font);
        String numString = "" + quantity;
        FontMetrics metrics = g2d.getFontMetrics();
        int drawWidth = metrics.stringWidth(numString);
        g2d.drawString(numString, getWidth() - drawWidth - 2, getHeight() - 2);
        g2d.dispose();
    }

    public static class ItemMouseAdapter extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            InventoryItemRenderer renderer = (InventoryItemRenderer) e.getSource();
            if (renderer.getInvItem() == null) {
                return;
            }
            TransferHandler handler = renderer.getTransferHandler();
            handler.exportAsDrag(renderer, e, TransferHandler.MOVE);
        }

    }

    public static interface ItemRendererListener {

        public void itemChanged(InventoryItem item);
    }

}
